package com.example.waterdispenserm2.mo.inductionmain;



import com.aill.androidserialport.SerialPort;
import com.example.waterdispenserm2.util.MyLog;

import android.util.Log;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;

    public class SerialPortHelper {
        private static final String TAG = "SerialPortHelper";
        private SerialPort serialPort;
        private InputStream inputStream;
        private OutputStream outputStream;
        private ReadThread readThread;
        private boolean isOpen = false;
        private OnDataReceivedListener listener;

        public interface OnDataReceivedListener {
            void onDataReceived(byte[] data);
        }

        public SerialPortHelper(OnDataReceivedListener listener) {
            this.listener = listener;
        }

        public boolean open(String devicePath, int baudRate) {
            try {
                serialPort = new SerialPort(new File(devicePath), baudRate, 0);
                inputStream = serialPort.getInputStream();
                outputStream = serialPort.getOutputStream();
                isOpen = true;

                readThread = new ReadThread();
                readThread.start();
                return true;
            } catch (Exception e) {
                Log.e(TAG, "串口打开失败: " + e.getMessage());
                return false;
            }
        }

        public void close() {
            try {
                if (readThread != null) {
                    readThread.interrupt();
                    readThread = null;
                }
                if (inputStream != null) inputStream.close();
                if (outputStream != null) outputStream.close();
                if (serialPort != null) serialPort.close();
                isOpen = false;
            } catch (IOException e) {
                Log.e(TAG, "串口关闭失败: " + e.getMessage());
            }
        }

        public void sendData(byte[] data) {
            if (!isOpen || outputStream == null) {
                Log.e(TAG, "串口未打开或输出流为空");
                return;
            }
            try {
                outputStream.write(data);
                outputStream.flush();
                MyLog.d(TAG, "发送数据: " + bytesToHex(data));
            } catch (IOException e) {
                Log.e(TAG, "数据发送失败: " + e.getMessage());
            }
        }

        private class ReadThread extends Thread {
            @Override
            public void run() {
                while (!isInterrupted() && isOpen) {
                    try {
                        if (inputStream == null) return;

                        byte[] buffer = new byte[256];
                        int size = inputStream.read(buffer);
                        if (size > 0 && listener != null) {
                            byte[] receivedData = Arrays.copyOf(buffer, size);
                            Log.d(TAG, "接收数据: " + bytesToHex(receivedData));
                            listener.onDataReceived(receivedData);
                        }
                    } catch (Exception e) {
                        Log.e(TAG, "数据接收失败: " + e.getMessage());
                        return;
                    }
                }
            }
        }

        private String bytesToHex(byte[] bytes) {
            StringBuilder sb = new StringBuilder();
            for (byte b : bytes) {
                sb.append(String.format("%02X ", b));
            }
            return sb.toString();
        }


}
